package com.safeneeds.linguimana.ser.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.safeneeds.linguimana.dto.NoticeDataStorePort;
import com.safeneeds.linguimana.dto.PortConstant;
import com.safeneeds.linguimana.entity.PortGlutNotice;
import com.safeneeds.linguimana.entity.dao.IPortGlutNoticeDao;
import com.safeneeds.linguimana.ser.IBgNoticeService;
import com.safeneeds.util.base.IUserInfo;
import com.safeneeds.util.other.StringUtil;
import com.safeneeds.util.page.PageInfo;
import com.safeneeds.util.page.ser.IPageService;

/**
 * 门户通告管理
 * @author ytw
 * @date 2017.2.21
 */

@Service
@Transactional
public class BgNoticeService implements IBgNoticeService{
	@Resource(name="portGlutNoticeDao")
	private IPortGlutNoticeDao portGlutNoticeDao;
	
	@Resource(name="pageService")
	private IPageService pageService;
	/**
	 * 获取新闻信息
	 */
	
	@Override
	public List<PortGlutNotice> getNotice() {
		List<PortGlutNotice> list=portGlutNoticeDao.getNotice(5);
		return list;
	}
	/**
	 * 添加或更新通知公告
	 */
	@Override
	public void addOrEditNotice(PortGlutNotice portGlutNotice,
			NoticeDataStorePort dataStore, IUserInfo userInfo) {
		// TODO Auto-generated method stub
		//新增
		if(StringUtil.isEmpty(portGlutNotice.getNoticeId())){
			addNoticeDo(portGlutNotice, dataStore, userInfo);
		//更新
		}else{
			editNoticeDo(portGlutNotice, dataStore, userInfo);
		}
		
		
	}
	/**编辑通知公告
	 * 讲用户编辑过的通知公告进行保存更改
	 * @param portGlutNotice
	 * @param dataStore
	 * @param userInfo
	 */
	private void editNoticeDo(PortGlutNotice portGlutNotice,
			NoticeDataStorePort dataStore, IUserInfo userInfo) {
		// TODO Auto-generated method stub
		//通过通知公告id获取通知公告信息
		PortGlutNotice noticeHis = portGlutNoticeDao.getNoticeByIdDao(portGlutNotice.getNoticeId());
		if(noticeHis == null){
			return ;
		}
		//更新通知公告
		noticeHis.setCate(portGlutNotice.getCate());
		noticeHis.setIssuer(userInfo.getUserName());//发布人
		noticeHis.setShow(portGlutNotice.getShow());
		noticeHis.setKeyWord(portGlutNotice.getKeyWord());
		noticeHis.setContent(portGlutNotice.getContent());
		noticeHis.setDescription(portGlutNotice.getDescription());
		noticeHis.setListdisplay(portGlutNotice.getListdisplay());
		noticeHis.setStick(portGlutNotice.getStick());
		if(portGlutNotice.getShow() == null){
			//默认显示  档点击保存草稿箱是 值为3
			noticeHis.setShow(PortConstant.NEWS_STATUS_SHOW);
		}
		
		if(portGlutNotice.getStick()==null){
			noticeHis.setStick(PortConstant.Notice_STICK_STATUS_DEFALUT);
		}
		if(portGlutNotice.getCate()==null){
			noticeHis.setCate(PortConstant.Notice_CATE_STATUS); 
		}
		Date nodate = new Date();
		noticeHis.setUpdateTime(nodate);
		System.out.println(noticeHis.toString());
		
		//将通知公告更新到到数据库
		portGlutNoticeDao.updateNotice(noticeHis);
	}
	/**
	 * 添加通知公告
	 * @param portGlutNotice
	 * @param dataStore
	 * @param userInfo
	 */
	public void addNoticeDo(PortGlutNotice portGlutNotice,
			NoticeDataStorePort dataStore, IUserInfo userInfo){

		portGlutNotice.setIssuer(userInfo.getUserName());//发布人
		portGlutNotice.setReadnum(new Long(0));
		if(portGlutNotice.getShow() == null){
			//默认显示  档点击保存草稿箱是 值为3
			portGlutNotice.setShow(PortConstant.NEWS_STATUS_SHOW);
		}
		
		if(portGlutNotice.getStick()==null){
			portGlutNotice.setStick(PortConstant.Notice_STICK_STATUS_DEFALUT);
		}
		if(portGlutNotice.getCate()==null){
			portGlutNotice.setCate(PortConstant.Notice_CATE_STATUS); 
		}
		Date nodate = new Date();
		portGlutNotice.setUpdateTime(nodate);
		System.out.println(portGlutNotice.toString());
		
		//将通知公告插入到数据库
		portGlutNoticeDao.addNotice(portGlutNotice);
	}
	/**
	 * 通知公告下的所有分类
	 * @return 
	 */
	@Override
	public List<String> getNoticeAllCate() {
		// TODO Auto-generated method stub
		
		return portGlutNoticeDao.getNoticeAllCateDao();
	}
	/**
	 * 获取分页数据
	 */
	@Override
	public PageInfo listNoticePage(PageInfo pageInfo,
			NoticeDataStorePort noticedataStore) {
		// TODO Auto-generated method stub
		pageInfo = portGlutNoticeDao.listNoticePageDao(pageInfo,noticedataStore);
		pageInfo = pageService.getSQLPageInfo(pageInfo);
		return pageInfo;
	}
	@Override
	public PortGlutNotice getNoticeById(String noticeId) {
		// TODO Auto-generated method stub
		return portGlutNoticeDao.getNoticeByIdDao(noticeId);
	}
	@Override
	public int updateNoticeShow(String ids, String isShow) {
		// TODO Auto-generated method stub
		
		return portGlutNoticeDao.updateNoticeShowDao(ids, isShow);
	}

}
